package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;

import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.SMSUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
//   实现体检预约方法
    @Override
    public Result order(Map map) throws Exception {
        Date date = DateUtils.parseString2Date((String) map.get("orderDate"));
        //检查当前日期是否进行了预约设置
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);

//        指定的日期没有进行预约设置，无法完成体检预约
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约日期是否预约已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number <= reservations){
//            已经约满，无法预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //防止重复预约
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);

        if (member!=null) {
            Integer id = member.getId();
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(id, date, Integer.parseInt(setmealId));
//            根据条件进行查询
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
//            说明用户在重复预约，无法完成再次预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            memberDao.edit(member);
        }else {
            //检查当前用户是否为会员，如果不是会员 在预约的同时自动注册为会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动注册
        }


        //可以预约，设置预约人数加一 向order表和member表追加数据
        Integer id = member.getId();
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String) map.get("orderType"));
        System.out.println((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        String[] telephones = (String[]) map.get("telephones");
        String[] params = (String[]) map.get("params");
        SMSUtils.sendShortMessage(SMSUtils.ORDER_SUCCESS,telephones,params);
        //保存预约信息到预约表
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map!=null) {
            String orderDate = DateUtils.parseDate2String((Date) map.get("orderDate"));
            map.put("orderDate",orderDate);
        }
        return map;
    }
}
